﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Hxj.Tools.EntityDesign.Model;
using System.Xml;
using System.Windows.Forms;
using Dos.ORM;
using Dos.ORM.Common;
using Hxj.Tools.EntityDesign;

namespace Dos.Tools.Common
{
    public class BLLModelBuilder
    {
        private List<Hxj.Tools.EntityDesign.Model.ColumnInfo> _columns = new List<Hxj.Tools.EntityDesign.Model.ColumnInfo>();

        private string _tableName;
        private string _dbType;

        private string _nameSpace = "BLL.Model";

        private string _className;

        private bool _isView = false;

        private bool _isSZMDX = false;
        private bool _isNewModel = false;
        private bool _isAdmin = false;
        public BLLModelBuilder(string tableName, string nameSpace, string className, List<Hxj.Tools.EntityDesign.Model.ColumnInfo> columns, bool isView, bool isSZMDX, string dbType = null, bool isNewModel = false, bool isAdmin = false)
        {
            _isSZMDX = isSZMDX;
            _className = Utils.ReplaceSpace(className);
            _nameSpace = Utils.ReplaceSpace(nameSpace);
            _tableName = tableName;
            _dbType = dbType;
            _isNewModel = isNewModel;
            _isAdmin = isAdmin;
            if (_isSZMDX)
            {
                _className = Utils.ToUpperFirstword(_className);
                TableName = Utils.ToUpperFirstword(TableName);
                //ViewName = Utils.ToUpperFirstword(ViewName);
            }
            _isView = isView;

            foreach (Hxj.Tools.EntityDesign.Model.ColumnInfo col in columns)
            {
                col.ColumnName = Utils.ReplaceSpace(col.ColumnName);
                if (_isSZMDX)
                {
                    col.ColumnName = Utils.ToUpperFirstword(col.ColumnName);
                }

                col.DeText = Utils.ReplaceSpace(col.DeText);
                _columns.Add(col);
            }
        }

        public List<Hxj.Tools.EntityDesign.Model.ColumnInfo> Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        public string NameSpace
        {
            get { return _nameSpace; }
            set { _nameSpace = value; }
        }
        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }
        public string DbType
        {
            get { return _dbType; }
            set { _dbType = value; }
        }
        public bool IsView
        {
            get { return _isView; }
            set { _isView = value; }
        }

        public string Builder()
        {
            Columns = DbToCS.DbtoCSColumns(Columns, DbType, true);

            StringPlus plus = new StringPlus();
            plus.AppendLine("//------------------------------------------------------------------------------");
            plus.AppendLine("// <auto-generated>");
            plus.AppendLine("//     此代码由工具生成。");
            plus.AppendLine("//     运行时版本:" + Environment.Version.ToString());
            plus.AppendLine("//     对此文件的更改可能会导致不正确的行为，并且如果");
            plus.AppendLine("//     重新生成代码，这些更改将会丢失。");
            plus.AppendLine("// </auto-generated>");
            plus.AppendLine("//------------------------------------------------------------------------------");
            plus.AppendLine();
            plus.AppendLine();
            plus.AppendLine("using System;");
            plus.AppendLine("using System.Data;");
            plus.AppendLine("using AutoMapper;");
            plus.AppendLine("using " + NameSpace + ".DAL.Models;");

            plus.AppendLine();
            plus.AppendLine("namespace " + NameSpace + (_isAdmin ? ".BLL.Admin.Models" : ".BLL.Models"));
            plus.AppendLine("{");
            plus.AppendLine();

            plus.AppendSpaceLine(1, "public  class " + ClassName);
            plus.AppendSpaceLine(1, "{");

            //内容
            plus.Append(GenModel());
            plus.AppendLine();
            //条件
            plus.Append(GenOrderBy());
            plus.AppendLine();

            plus.Append(GenConvert());
            plus.AppendLine();

            plus.AppendSpaceLine(1, "}");
            plus.AppendLine("}");
            plus.AppendLine("");
            return plus.ToString();
        }
        private string GenModel()
        {
            StringPlus plus = new StringPlus();

            plus.AppendSpaceLine(2, "#region Model");
            foreach (ColumnInfo column in Columns)
            {
                plus.AppendSpaceLine(2, "/// <summary>");
                plus.AppendSpaceLine(2, "/// " + column.DeText);
                plus.AppendSpaceLine(2, "/// </summary>");
                //plus.AppendSpaceLine(2, "public " + column.TypeName=="int"?@"int?":column.TypeName + " " + column.ColumnName + " {get;set;}");
                string typename = column.TypeName;
                if (typename == "int")
                {
                    typename += "?";
                }

                plus.AppendSpaceLine(2, "public " + typename + " " + column.ColumnName + " {get;set;}");
                if (typename.ToLower().Contains("datetime"))
                {
                    //添加时间起始结束条件
                    plus.AppendSpaceLine(2, "public " + typename + " " + column.ColumnName + "_Start {get;set;}");
                    plus.AppendSpaceLine(2, "public " + typename + " " + column.ColumnName + "_End {get;set;}");
                }
            }

            plus.AppendSpaceLine(2, "#endregion");
            return plus.ToString();
        }

        private string GenOrderBy()
        {
            StringPlus plus = new StringPlus();
            plus.AppendSpaceLine(2, "#region OrderBy");
            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 排序条件");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public " + TableName + "OrderByEnum OrderBy");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(2, "get { return _orderBy; }");
            plus.AppendSpaceLine(2, "set { _orderBy = value; }");
            plus.AppendSpaceLine(2, "}");
            plus.AppendSpaceLine(2, "private " + TableName + "OrderByEnum _orderBy = " + TableName + "OrderByEnum.Null;");
            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 排序条件选项");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public enum " + TableName + "OrderByEnum");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "Null=0,");
            int n = 1;
            foreach (ColumnInfo column in Columns)
            {
                plus.AppendSpaceLine(2, "/// <summary>");
                plus.AppendSpaceLine(2, "/// " + column.DeText);
                plus.AppendSpaceLine(2, "/// </summary>");
                plus.AppendSpaceLine(3, column.ColumnName + "=" + n.ToString() + ",");
                n++;
            }
            plus.AppendSpaceLine(2, "}");
            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 是否倒序");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public bool OrderByDesc { get { return _orderByDesc; } set { _orderByDesc = value; } }");
            plus.AppendSpaceLine(2, "private bool _orderByDesc = false;");



            plus.AppendSpaceLine(2, "#endregion");



            return plus.ToString(); ;



        }

        string GenConvert()
        {
            StringPlus plus = new StringPlus();
            plus.AppendSpaceLine(2, "#region Convert");

            plus.AppendSpaceLine(2, "public " + TableName + "_Condition ToCondition()");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "return Mapper.Map<" + TableName + "_Condition>(this);");
            plus.AppendSpaceLine(2, "}");

            plus.AppendSpaceLine(2, "public " + TableName + " ToDALModel()");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "return Mapper.Map<" + TableName + ">(this);");
            plus.AppendSpaceLine(2, "}");


            plus.AppendSpaceLine(2, "#endregion");

            return plus.Value;
        }
    }
}
